package com.rabbit01.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 发布确认（高级）
 *  生产者发送消息 rabbitmq宕机情况
 */
@Configuration
public class ConfirmConfig {

    //交换机
    public static final String CONFIRM_EXCHANGE="confirmExchange";
    //队列
    public static final String CONFIRM_QUEUE="confirm.queue";
    //routingkey
    public static final String CONFIRM_ROUTING_KEY="key1";

    //备份交换机 （备份交换机可以直接将消息转发到备份交换机中进行接收，不需要回调，有回退和配置备份交换机 备份优先级最高）
    public static final String BACKUP_EXCHANGE="backup_exchange";
    public static final String BACKUP_QUEUE="backup_queue";
    //报警队列
    public static final String WARNING_QUEUE="warning_queue";




    //创建一个交换机
    @Bean
    public DirectExchange confirmExchange(){
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE).durable(true).withArgument("alternate-exchange",BACKUP_EXCHANGE).build();
    }

    @Bean
    public Queue confirmQueue(){
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }

    @Bean
    public Binding queueBindingExchange(@Qualifier("confirmExchange")DirectExchange exchange,@Qualifier("confirmQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_ROUTING_KEY);
    }


    //备份交换机创建
    @Bean
    public FanoutExchange backupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE);
    }

    @Bean
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }

    @Bean
    public Queue warningQueue(){ return QueueBuilder.durable(WARNING_QUEUE).build();}

    //绑定
    @Bean
    public Binding backupQueueBindingbackupExchange(@Qualifier("backupExchange")FanoutExchange exchange,@Qualifier("backupQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding warningQueueBindingbackupExchange(@Qualifier("backupExchange")FanoutExchange exchange,@Qualifier("warningQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }

}
